import logging
from pylons import request, response, session
from pylons import tmpl_context as c
from pylons.controllers.util import abort, redirect_to

from simplesite.lib.base import BaseController, render
import simplesite.model as model

import simplesite.model.meta as meta
import simplesite.lib.helpers as h

import formencode
from formencode import htmlfill
from pylons.decorators import validate
from pylons.decorators.rest import restrict

import webhelpers.paginate as paginate

from authkit.permissions import ValidAuthKitUser
from authkit.authorize.pylons_adaptors import authorize

#from simplesite import model

log = logging.getLogger(__name__)
class NewCommentForm(formencode.Schema):
    allow_extra_fields = True
    filter_extra_fields = True
    name = formencode.validators.String(not_empty=True)
    email = formencode.validators.Email(not_empty=True)
    content = formencode.validators.String(
        not_empty=True,
        messages={
            'empty':'Please enter a comment.'
        }
    )

class CommentController(BaseController):
    def __before__(self, action, pageid=None):
         page_q = meta.Session.query(model.Page)
         c.page = page_q.filter_by(id=int(pageid)).first()
         if c.page is None:
             abort(404)
 
    def view(self, id=None):
        if id is None:
            abort(404)
        comment_q = meta.Session.query(model.Comment)
        c.comment = comment_q.filter_by(pageid=c.page.id, id=int(id)).first()
        if c.comment is None:
            abort(404)
        return render('/derived/comment/view.html')
    
    
    def index(self):
        # Return a rendered template
        #   return render('/template.mako')
        # or, Return a response
        return 'Hello World'
    def list(self):
        comments_q = meta.Session.query(model.Comment)
        c.paginator = paginate.Page(
            comments_q,
            page=int(request.params.get('page', 1)),
            items_per_page = 10,
            controller='comment',
            action='list'
        )
        return render('/derived/comment/list.html')
    @restrict('POST')
    @validate(schema=NewCommentForm(), form='new')
    def create(self):
            # Add the new comment to the database
            comment = model.Comment()
            for k, v in self.form_result.items():
                setattr(comment, k, v)
            comment.pageid = c.page.id
            meta.Session.save(comment)
            meta.Session.commit()
            # Issue an HTTP redirect
            return redirect_to(pageid=c.page.id, controller='comment', action='view', id=comment.id)
         
            
    @authorize(ValidAuthKitUser())
    def edit(self, id=None):
        if id is None:
            abort(404)
        comment_q = meta.Session.query(model.Comment)
        
        c.comment = comment_q.filter_by(pageid=c.page.id, id=id).first()
        
        
        if comment is None:
            abort(404)
        values = {
           'name': comment.name,
           'email': comment.email,
           'content': comment.content,
           
        }
        return htmlfill.render(render('/derived/comment/edit.html'), values)
    
        
            
    def new(self):
            return render('/derived/comment/new.html')
    